// 新增用户
$("#addUserBtn").on("click", function () {
    var userName = $(".userName").val();
    var userPassword = $(".userPassword").val();
    if (userName != null && userName.length != 0) {
        // 检查用户是否已注册
        $.ajax({
            type: "GET",
            url: "/userManage/checkUserName?userName=" + userName,
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    if (userPassword != null && userPassword.length != 0) {
                        $.ajax({
                            type: "POST",
                            url: "/userManage/addUser",
                            data: $("#addUserForm").serialize(),
                            dataType: "json",
                            success: function (data) {
                                if (data.result == "success") {
                                    alert("添加成功");
                                    window.location.reload();
                                } else {
                                    alert("添加失败");
                                }
                            },
                            error: function () {
                                alert("服务器错误，操作失败");
                            }
                        });
                    } else {
                        alert("验证失败：密码不能为空，且满足6~16位数字、字母");
                    }
                } else {
                    alert("验证失败：该用户名已注册");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    } else {
        alert("验证失败：用户名不能为空，且满足2~7位中文，强烈建议输入真实姓名");
    }
});

// 搜索用户
$("#serarchUserBtn").on("click", function () {
    var type = $(".searchType").val();
    var content = $(".searchContent").val();
    if (content != null && content.length != 0) {
        window.location.href = "/userManage/searchUser?type=" + type + "&content=" + content;
    } else {
        alert("请输入查询条件");
    }
});

// 更新用户
$("#updateUserBtn").on("click", function () {
    var userName = $(".userName").val();
    var userPassword = $(".userPassword").val();
    if (userName != null && userName.length != 0) {
        if (userPassword != null && userPassword.length != 0 ) {
            $.ajax({
                type: "POST",
                url: "/userManage/updateUser",
                data: $("#updateUserForm").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.result == "success") {
                        alert("更新成功");
                        window.location.reload();
                    }
                },
                error: function () {
                    alert("服务器错误，操作失败");
                }
            });
        } else {
            alert("密码不能为空");
        }
    } else {
        alert("用户名不能为空");
    }
});

// 删除用户
function delUserFun(userId) {
    $("#deleteUserBtn").on("click", function () {
        $.ajax({
            type: "GET",
            url: "/userManage/deleteUser?userId=" + userId,
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    alert("删除成功");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    });
}

